//
//  UITextField+Extension.swift
//  GWCDriver
//
//  Created by Apple on 2017/5/6.
//  Copyright © 2017年 The_X. All rights reserved.
//

import UIKit

extension UITextField {
    
    convenience init(es_title: String? = nil,
                     holder: String? = nil,
                     font: CGFloat = 14.5,
                     titleColor: UIColor = UIColor.darkText,
                     holderColor: UIColor = UIColor.lightGray,
                     keyBoard: UIKeyboardType = .default,
                     alignment: NSTextAlignment = .left) {
        
        self.init()
        
        self.text = es_title

        self.font = UIFont.systemFont(ofSize: font)
        self.textColor = titleColor
        
        self.placeholder = holder
        self.setValue(holderColor, forKeyPath: "_placeholderLabel.textColor")
        
        self.textAlignment = alignment
        
        self.setAutoAdjustKeyboard(autoAdjust: true)
        
        self.keyboardType = keyBoard
    }

    func setAutoAdjustKeyboard(autoAdjust: Bool) {
        
        if autoAdjust {
        
            // 注册键盘将要出现与将要隐藏的通知事件
            NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
            
            NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
        } else {
        
            NotificationCenter.default.removeObserver(self)
        }
    }
    
    @objc fileprivate func keyboardWillShow(notification: NSNotification) {
        
        if self.isFirstResponder {
            
        let relativePoint = self.convert(CGPoint.zero, to: UIApplication.shared.keyWindow)
        
        let keyboardHeight = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? CGRect)?.size.height
        
        let actualHeight = self.frame.height + relativePoint.y + keyboardHeight!
            
        let overstop = actualHeight - UIScreen.main.bounds.height + 20
            
            if overstop > 0 {
                
                UIView.animate(withDuration: 0.25) {
                
                    UIApplication.shared.keyWindow?.frame.origin.y = UIScreen.main.bounds.origin.y - overstop
                }
            }
        }
    }
    
    @objc func keyboardWillHide() {
        
        if self.isFirstResponder {
            
            UIView.animate(withDuration: 0.25) {
                
                UIApplication.shared.keyWindow?.frame = UIScreen.main.bounds
            }
        }
    }
}

extension UITextView {

    func setAutoAdjustKeyboard(autoAdjust: Bool) {
        
        if autoAdjust {
            
            // 注册键盘将要出现与将要隐藏的通知事件
            NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
            
            NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
        } else {
            
            NotificationCenter.default.removeObserver(self)
        }
    }
    
    @objc fileprivate func keyboardWillShow(notification: NSNotification) {
        
        if self.isFirstResponder {
            
            let relativePoint = self.convert(CGPoint.zero, to: UIApplication.shared.keyWindow)
            
            let keyboardHeight = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? CGRect)?.size.height
            
            let actualHeight = self.frame.height + relativePoint.y + keyboardHeight!
            
            let overstop = actualHeight - UIScreen.main.bounds.height + 20
            
            if overstop > 0 {
                
                UIView.animate(withDuration: 0.25) {
                    
                    UIApplication.shared.keyWindow?.frame.origin.y = UIScreen.main.bounds.origin.y - overstop
                }
            }
        }
    }
    
    @objc func keyboardWillHide() {
        
        if self.isFirstResponder {
            
            UIView.animate(withDuration: 0.25) {
                
                UIApplication.shared.keyWindow?.frame = UIScreen.main.bounds
            }
        }
    }
}
